import store from "@/store";
import {
	timeFormat
} from './util.js'
export default {
	install(Vue) {

		// 先执行 onLaunch 后 在执行页面 onLoad
		Vue.prototype.$onLaunched = new Promise(resolve => {
			Vue.prototype.$isResolve = resolve
		})

		// 密钥
		Vue.prototype.$secretkey = '5560E5492F8420EFDDA3E4FA06AEA249';

		//时间戳
		Vue.prototype.$timestamp = String(Date.parse(new Date()) / 1000);

		// md5加密
		Vue.prototype.$mad = (...params) => {
			return md5(md5(params.join("") + _this.$secretkey + _this.$timestamp));;
		};
		//data中的参数
		Vue.prototype.$sign = (data = {}) => {
			data.sign = _this.$mad(...Object.values(data));
			data.timestamp = _this.$timestamp;
			return data;
		};

		/**
		 * @param {Date} value
		 * @param {String} format  yyyy-mm-dd hh:MM:ss
		 * @description 日期格式化
		 */
		Vue.prototype.$dateFormat = (value, format = "yyyy-mm-dd") => {
			return timeFormat(value, format);
		};

		// 获取当前打开过的页面路由
		Vue.prototype.$getPagePath = () => {
			let routes = getCurrentPages(); // 获取当前打开过的页面路由数组
			let curRoute = routes[routes.length - 1].route; //获取当前页面路由
			let curParam = routes[routes.length - 1].options; //获取路由参数
			// 拼接参数
			if (Object.getOwnPropertyNames(curParam).length != 0) {
				let param = '';
				for (let key in curParam) {
					param += '&' + key + '=' + curParam[key];
				}
				var path = curRoute + '?' + param.slice(1);
				return uni.setStorageSync('path', '/' + path);
			} else {
				return uni.setStorageSync('path', '/' + curRoute);
			}
		};

		/**
		 * @param {String} url
		 * @description 统一跳转
		 */
		Vue.prototype.navigateTo = (url) => {
			uni.navigateTo({
				url: url,
				animationType: 'pop-in',
				animationDuration: 300
			})
		};

		/**
		 *  关闭当前页面并跳转
		 */
		Vue.prototype.redirectTo = (url) => {
			// judgeLogin(() => {
			uni.redirectTo({
				url: url,
				animationType: 'pop-in',
				animationDuration: 300
			})
			// })
		};

		Vue.prototype.reLaunch = (url) => {
			uni.reLaunch({
				url: url,
				animationType: 'pop-in',
				animationDuration: 300
			})
		};

		Vue.prototype.switchTab = (url) => {
			uni.switchTab({
				url: url,
				animationType: 'pop-in',
				animationDuration: 300
			})
		};
		// 返回上一页
		Vue.prototype.navigateBack = (time, num) => {
			time = time ? time : 1500;
			num = num ? num : 1;
			return setTimeout(res => {
				uni.navigateBack({
					delta: Number(num)
				})
			}, time);
		};

		// 显示消息提示框。
		Vue.prototype.$toast = (value, icon) => {
			icon = icon ? icon : 'none';
			uni.showToast({
				title: value,
				duration: 2000,
				icon
			});
		};
		// 显示 loading 提示框
		Vue.prototype.$showLoading = (value) => {
			uni.showLoading({
				title: value
			});
		};
		Vue.prototype.$hideLoading = () => {
			setTimeout(function() {
				uni.hideLoading();
			}, 1000);
		};
		// 根据数据字典 展示枚举内容
		Vue.prototype.$dictByKey = (dict, keys, keycode, keyname) => {
			keycode = keycode ? keycode : "value";
			keyname = keyname ? keyname : "label";
			let key = keys;
			if ((key == null || key == "" || key == undefined) && key != 0) {
				return '';
			}

			key = key != 0 ? String(key).split(',') : '0';
			let names = [];
			if (key.length != 0) {
				if (Array.isArray(dict)) {
					for (let i = 0; i < dict.length; i++) {
						for (let k = 0; k < key.length; k++) {
							if (dict[i][keycode] == key[k]) {
								names.push(dict[i][keyname]);
							}
						}
					}
				} else {
					for (let i in dict) {
						for (let k = 0; k < key.length; k++) {
							if (i == key[k]) {
								names.push(dict[i]);
							}
						}
					}

				}
				return names.join(',');
			} else if (key == 0) {
				for (let i in dict) {
					if (i == 0) {
						names.push(dict[i][keyname]);
					}
				}
				return names.join(',');
			} else {
				return '';
			}
		};

	}
}